//
//  AppDelegate.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/10.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        
        registerNotification()
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        setThemeColor()
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController()
        
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    //注册通知
    private func registerNotification(){
        //object = nil  这个时候发出的是一条   '广播'
        //object  != nil  发出的是一个一对一 '通知'
        //target - action  机制 是OC中的机制 由于 swift 可以直接调用 OC
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "choseRootVC:", name: WBSwitchRootVC, object: nil)
  
    }
    
    //通知的响应方法 中 选择 到底是显示  tabbarVC 还是 欢迎界面
    @objc private func choseRootVC(n: NSNotification){
        
        print(n.object)
        
        if n.object != nil {
            //欢迎界面
            window?.rootViewController = WelcomeViewController()
            
        } else {
            window?.rootViewController = MainTabBar()
        
        }
//        window?.rootViewController = (n.object == nil ? MainTabBar():WelcomeViewController())
    }
    
    //移除通知  编码规范而已
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    
    //根据用户是否登录 指定默认的根视图控制器
    private func defaultRootViewController() -> UIViewController {
    
//        if UserAccountViewModel().userLogin {
//            return WelcomeViewController()
//        }
//    
//        return MainTabBar()
        
        return UserAccountViewModel().userLogin ? WelcomeViewController():MainTabBar()
    
    }
    

    //需要尽早设置 必须在试图实例化之前进行设置
    private func setThemeColor() {
    
        //导航试图控制器的全局色 是红色
        UINavigationBar.appearance().tintColor = UIColor.redColor()

//        UITabBar.appearance().tintColor = UIColor.redColor()
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

